package com.fang.uploadFile.controll;


import com.aliyun.oss.OSSClient;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.util.*;

@RestController
@RequestMapping("/file")
public class UploadFileToAliOSS {


    @PostMapping("/upload")
    public Map<String, URL> uploadFile(@RequestParam("file") MultipartFile[] files, HttpServletResponse response, HttpServletRequest request) {



        // Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = "https://oss-eu-west-1.aliyuncs.com";
        // 使用刚刚创建的accessKeyId和accessKeySecret
        String accessKeyId = "LTAI4Fwf25nq38P6ckofuDgV";
        String accessKeySecret = "UOwSemxqu3KKPY0YDQqya4dcthZuWl";
        String bucketName = "shop-data-beta";


        Map<String, URL> strings = new HashMap<>();

        for (MultipartFile file : files) {
            File file1 = saveFile(file);

        }


        return strings;
    }


    private File saveFile(MultipartFile file) {

        File uploadPic = null;
        try {
            uploadPic = new java.io.File("/home/fang/Music/" + file.getOriginalFilename());
            if (!uploadPic.exists()) {
                uploadPic.mkdirs();
            }
            file.transferTo(uploadPic);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return uploadPic;
    }


}
